//
//  ShopBaseInfoVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/15.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "ShopBaseInfoVC.h"

@interface ShopBaseInfoVC ()<UITextFieldDelegate,UITextViewDelegate>{
    UIButton *_updateAdress;
    UIButton *_updatetell;
}
@property (strong, nonatomic)  UILabel *storeName;
@property (strong, nonatomic)  UITextView *storeAdrres;
@property (strong, nonatomic)  UITextField *storeiPhone;
@property (strong, nonatomic)  UIButton *updateBtn;

@end

@implementation ShopBaseInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
      [self initView];
     [self setNavTitle:@"基本信息" titleColor:[UIColor blackColor] font:font17];
    NSDictionary *useInfo = [userDefaults objectForKey:@"agentInfo"];
    _storeName.text = useInfo[@"agent_name"];
    _storeAdrres.text = useInfo[@"address"];
    _storeiPhone.text = useInfo[@"telephone"];
    if(_storeiPhone.text.length>0){
      _storeiPhone.enabled = NO;
        _updatetell.selected = NO;
    }

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView{
    UILabel *dianp = [[UILabel alloc] init];
    dianp.text = @"店铺名称";
    dianp.font = font14;
    [self.view addSubview:dianp];
    dianp.sd_layout.topSpaceToView(self.view, GETHEIGHT(60)).leftSpaceToView(self.view, GETWITH(50)).widthIs(0).autoHeightRatio(0);
    [dianp setSingleLineAutoResizeWithMaxWidth:100];
    
    _storeName = [[UILabel alloc] init];
    _storeName.font = font14;
    [self.view addSubview:_storeName];
    _storeName.sd_layout.leftSpaceToView(dianp, GETWITH(44)).rightSpaceToView(self.view, GETWITH(88)).topEqualToView(dianp).autoHeightRatio(0);
    
    UILabel *dianDZ = [[UILabel alloc] init];
    dianDZ.text = @"店铺地址";
    dianDZ.font = font14;
    [self.view addSubview:dianDZ];
    dianDZ.sd_layout.topSpaceToView(_storeName, GETHEIGHT(50)).leftSpaceToView(self.view, GETWITH(50)).widthIs(0).autoHeightRatio(0);
    [dianDZ setSingleLineAutoResizeWithMaxWidth:100];
    
    _storeAdrres = [[UITextView alloc] init];
    _storeAdrres.font = font14;
    _storeAdrres.editable = NO;
    _storeAdrres.delegate = self;
    _storeAdrres.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:_storeAdrres];
    _storeAdrres.sd_layout.leftSpaceToView(dianDZ, GETWITH(44)).rightSpaceToView(self.view, GETWITH(140)).topEqualToView(dianDZ).heightIs(GETHEIGHT(70));
    
    
    _updateAdress = [UIButton buttonWithType:UIButtonTypeCustom];
    [_updateAdress setImage:[UIImage imageNamed:@"xinxi_icon_default_nor.png"] forState:UIControlStateNormal];
    [_updateAdress setImage:[UIImage imageNamed:@"xinxi_icon2_default_nor.png"] forState:UIControlStateSelected];
    [_updateAdress addTarget:self action:@selector(updatAdree:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_updateAdress];
    _updateAdress.sd_layout.rightSpaceToView(self.view, GETWITH(68)).topEqualToView(_storeAdrres).widthIs(GETWITH(60)).heightEqualToWidth();

    
    UILabel *dianIphone = [[UILabel alloc] init];
    dianIphone.text = @"店铺电话";
    dianIphone.font = font14;
    [self.view addSubview:dianIphone];
    dianIphone.sd_layout.topSpaceToView(_storeAdrres, GETHEIGHT(66)).leftSpaceToView(self.view, GETWITH(50)).widthIs(0).autoHeightRatio(0);
    [dianIphone setSingleLineAutoResizeWithMaxWidth:100];
    
    _storeiPhone = [[UITextField alloc] init];
    _storeiPhone.font = font14;
    _storeiPhone.delegate = self;
    _storeiPhone.returnKeyType = UIReturnKeyDone;
    _storeiPhone.placeholder = @"请输入手机号";
    
    [self.view addSubview:_storeiPhone];
    _storeiPhone.sd_layout.leftSpaceToView(dianIphone, GETWITH(44)).rightSpaceToView(self.view, GETWITH(140)).topEqualToView(dianIphone).heightIs(GETHEIGHT(40));
    
    _updatetell = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_updatetell setImage:[UIImage imageNamed:@"xinxi_icon_default_nor.png"] forState:UIControlStateNormal];
    [_updatetell setImage:[UIImage imageNamed:@"xinxi_icon2_default_nor.png"] forState:UIControlStateSelected];
    [_updatetell addTarget:self action:@selector(updateiPhone:) forControlEvents:UIControlEventTouchUpInside];
    _updatetell.selected = YES;
    [self.view addSubview:_updatetell];
    _updatetell.sd_layout.rightSpaceToView(self.view, GETWITH(68)).widthIs(GETWITH(60)).heightEqualToWidth().centerYEqualToView(_storeiPhone);
    

    
    _updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_updateBtn setBackgroundImage:[UIImage imageNamed:@"user_btn_default_nor.png"] forState:UIControlStateNormal];
    [_updateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_updateBtn setTitle:@"提交" forState:UIControlStateNormal];
        _updateBtn.titleLabel.font = font15;
    [_updateBtn addTarget:self action:@selector(updateStoreInfo:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_updateBtn];
    _updateBtn.sd_layout.topSpaceToView(_storeiPhone, GETHEIGHT(120)).rightEqualToView(_updatetell).heightIs(GETHEIGHT(92)).leftEqualToView(dianIphone);
    self.view.backgroundColor = [UIColor whiteColor];
    

    
}

- (void)updatAdree:(UIButton *)btn{
    btn.selected = !btn.selected;
    if(btn.selected){
         _storeAdrres.editable = YES;
         [_storeAdrres becomeFirstResponder];
    }else{
          _storeAdrres.editable = NO;
    }
}

- (void)updateiPhone:(UIButton *)btn{
    btn.selected = !btn.selected;
    if(btn.selected){
        _storeiPhone.enabled = YES;
        [_storeiPhone becomeFirstResponder];
    }else{
        _storeiPhone.enabled = NO;
    }
}

- (void)updateStoreInfo:(UIButton*)sender {
    if(![ToolClass valiMobile:_storeiPhone.text]){
        [AlertUtils showToastWithMessage:@"请填入正确的电话格式" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        return;
    }
    if(_storeAdrres.text.length<1){
        return;
    }
    if(_storeiPhone.text.length<1){
        return;
    }
    __weak typeof(self)weakself = self;
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定要修改店铺信息？" preferredStyle:UIAlertControllerStyleAlert];
    [alter addAction:[UIAlertAction actionWithTitle:@"我再想想" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alter addAction:[UIAlertAction actionWithTitle:@"确认修改" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakself updateShopInfo];
    }]];
    
    [self presentViewController:alter animated:YES completion:nil];
}


- (void)updateShopInfo{
   NSDictionary *dic = [userDefaults objectForKey:@"agentInfo"];
    
    __weak typeof(self)weakself = self;
    NSDictionary *pramar = @{@"address":_storeAdrres.text,@"telephone":_storeiPhone.text};
    
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/ShopInfo/changeInfo" parameter:pramar successBlock:^(PPHttpRequest *request, id responseObj) {
        [AlertUtils showToastWithMessage:@"修改成功" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        NSMutableDictionary *newInfo = dic.mutableCopy;
        [newInfo setObject:responseObj[@"address"] forKey:@"address"];
         [newInfo setObject:responseObj[@"telephone"] forKey:@"telephone"];
        [userDefaults setObject:newInfo forKey:@"agentInfo"];
        [weakself.navigationController popViewControllerAnimated:YES];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    _updatetell.selected = NO;
    _storeiPhone.enabled = NO;
    return YES;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
    //如果为回车则将键盘收起
    if ([text isEqualToString:@"\n"]) {
        _storeAdrres.editable = NO;
        _updateAdress.selected = NO;
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
